package com.drondea.testclient.handler.sgip.server;

import com.drondea.sms.conf.sgip.SgipServerSocketConfig;
import com.drondea.sms.session.sgip.SgipServerSessionManager;
import com.drondea.sms.type.GlobalConstants;
import com.drondea.sms.type.UserChannelConfig;
import com.drondea.testclient.panel.SgipPanel;
import lombok.extern.slf4j.Slf4j;

/**
 * @version V3.0.0
 * @description 服务器端简单测试
 * @author liyuehai
 * @date 2020年06月10日18:48
 **/

@Slf4j
public class SgipServer {

    private static final SgipPanel PANEL = SgipPanel.getInstance();
    private static final SgipServer SGIP_SERVER = new SgipServer();

    private SgipServer() {
    }

    public static SgipServer getInstance() {
        return SGIP_SERVER;
    }


    public void start(int port) {
        GlobalConstants.METRICS_ON = false;
        GlobalConstants.METRICS_CONSOLE_ON = false;
        SgipServerSocketConfig socketConfig = new SgipServerSocketConfig("test", port);
        socketConfig.setIdleTime(60);
        SgipServerCustomHandler customHandler = new SgipServerCustomHandler();

        SgipServerSessionManager sessionManager = new SgipServerSessionManager(name -> {
            if (name.startsWith(PANEL.userNameTextField.getText())) {
                UserChannelConfig userChannelConfig = new UserChannelConfig();
                userChannelConfig.setUserName(name);
                userChannelConfig.setPassword(PANEL.passwordTextField.getText());
                return userChannelConfig;
            } else {
                log.info("用户匹配失败");
                return null;
            }

        }, socketConfig, customHandler);
        sessionManager.doOpen();
        log.info("Sgip服务器监听[" + port + "]启动成功");
    }

}
